package com.dbzf.dao.impl;

import com.dbzf.dao.BaseDao;
import com.dbzf.dao.StudentDao;
import com.dbzf.entity.Student;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> getAllStu(Integer currentPage,Integer pageSize) {
        List<Student> stuList = new ArrayList<>();
        String sql = "select * from student limit ?,?";
        Object[] pram = {(currentPage-1)*pageSize,pageSize};
        ResultSet resultSet = BaseDao.getResultSet(sql, pram);
        try {
            Student student;
            while (resultSet.next()){
                student = new Student();
                student.setSex(resultSet.getInt("sex"));
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setStudy_number(resultSet.getString("study_number"));
                student.setAddress(resultSet.getString("address"));
                student.setPhone(resultSet.getString("phone"));
                stuList.add(student);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(BaseDao.connection,BaseDao.stmt,resultSet);
        }
        return stuList;
    }

    @Override
    public Student getStuByid(Integer id) {
        Student student = new Student();
        String sql =  "select * from student where id = ?";
        Object[] pram = {id};
        ResultSet resultSet = BaseDao.getResultSet(sql, pram);
        try {
            if(resultSet.next()){
                student.setSex(resultSet.getInt("sex"));
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setStudy_number(resultSet.getString("study_number"));
                student.setAddress(resultSet.getString("address"));
                student.setPhone(resultSet.getString("phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(BaseDao.connection,BaseDao.stmt,resultSet);
        }
        return student;
    }

    @Override
    public int addStu(Student student) {
        String sql = "insert into student values(null,?,?,?,?,?);";
        Object[] pram = {student.getStudy_number(),student.getName(),student.getSex(),student.getPhone(),student.getAddress()};
        int result = BaseDao.excute(sql, pram);
        BaseDao.closeAll(BaseDao.connection,BaseDao.stmt,null);
        return result;
    }

    @Override
    public int deleteStu(Integer sid) {
        String sql = "delete from student where student.id = ?;";
        Object[] pram = {sid};
        int result = BaseDao.excute(sql, pram);
        BaseDao.closeAll(BaseDao.connection,BaseDao.stmt,null);
        return result;
    }

    @Override
    public int updateStu(Student student) {
        String sql = "update student set address = ?,name = ?,sex = ?,phone = ?,study_number = ? where id = ?;";
        Object[] pram = {student.getAddress(),student.getName(),student.getSex(),student.getPhone(),student.getStudy_number(),student.getId()};
        int result = BaseDao.excute(sql, pram);
        BaseDao.closeAll(BaseDao.connection,BaseDao.stmt,null);
        return result;


    }

    @Override
    public int deleteStuMore(String ids) {
        StringBuilder stb = new StringBuilder();
        stb.append("(");
        stb.append(ids);
        stb.append(")");
        String sql = "delete from student where student.id in"+stb.toString();
        int result = BaseDao.excute(sql, null);
        BaseDao.closeAll(BaseDao.connection,BaseDao.stmt,null);
        return result;
    }

    @Override
    public int getStuCount() {
        String sql  = "select count(1) from student";
        ResultSet resultSet = BaseDao.getResultSet(sql, null);
        int result = 0;
        try {
            if(resultSet.next()){
                result = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(BaseDao.connection,BaseDao.stmt,resultSet);
        }

        return result;
    }
}
